package com.lattice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.lattice.dao.LatticeNoteManagerDao;
import com.lattice.dao.convert.LatticeNoteManagerDaoConvert;
import com.lattice.entity.dto.LatticeNoteUploadFileDTO;
import com.lattice.entity.dto.LatticeQueryDTO;
import com.lattice.entity.po.LatticeResultPO;
import com.lattice.entity.vo.LatticeResultVO;
import com.lattice.service.LatticeNoteManagerService;
import com.lattice.utils.AutoGenerateIds;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2022/11/20
 **/
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class LatticeNoteManagerServiceImpl implements LatticeNoteManagerService {

    @Resource
    private LatticeNoteManagerDao latticeNoteManagerDao;

    @Override
    public LatticeResultVO getAllLatticeNoteByPage(LatticeQueryDTO dto) {
        log.info("get all lattice by page param:{}",dto);
        dto.setPage(dto.getPage() - 1);
        List<LatticeResultPO> latticeList = latticeNoteManagerDao.getAllLatticeNoteByPage(dto);
        LatticeResultVO vo = new LatticeResultVO();
        vo.setLatticeResultList(latticeList);
        vo.setTotals(latticeList.size());
        return vo;
    }

    @Override
    public Boolean addLatticeNoteInfo(LatticeNoteUploadFileDTO latticeNoteUploadFileDTO) {
        log.info("add lattice note param:{}",latticeNoteUploadFileDTO);
        LatticeResultPO latticeResultPO = LatticeNoteManagerDaoConvert.INSTANCE.dto2po(latticeNoteUploadFileDTO);
        latticeResultPO.setLatticeId(AutoGenerateIds.getGenerateId());
        //由于mybatis plus的驼峰检测，手动赋值
        latticeResultPO.setLatticeSkipWebsite(latticeNoteUploadFileDTO.getLatticeSkipWebsite());
        latticeResultPO.setVersion(0);
        latticeResultPO.setUpdateTime(new Date());
        return latticeNoteManagerDao.insert(latticeResultPO) > 0;
    }

    @Override
    public Boolean updateLatticeNoteInfo(LatticeResultPO latticeResultPO ) {
        log.info("update lattice param:{}",latticeResultPO);
        return latticeNoteManagerDao.updateLatticeNoteInfo(latticeResultPO);
    }

    @Override
    public Boolean deleteLatticeNoteInfo(Long latticeId) {
        return latticeNoteManagerDao.deleteLatticeNoteInfo(Collections.singletonList(latticeId));
    }
}
